/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package argtrust.belief;

import java.util.Set;
import com.google.common.collect.Multimap;
import com.google.common.collect.HashMultimap;


import aima.core.logic.fol.parsing.ast.Sentence;
import argtrust.logic.InferenceRule;
import argtrust.trustnet.Agent;
import argtrust.measure.Measure;
/**
 *
 * @author Yuqing Tang
 */
public class BeliefBase {

    Multimap<Agent, Belief> mBeliefTable;
    Multimap<Sentence, Belief> mFactAgentTable;
    Multimap<InferenceRule, Belief> mRuleAgentTable;

    public BeliefBase() {
        mBeliefTable = HashMultimap.<Agent, Belief>create();
        mFactAgentTable = HashMultimap.<Sentence, Belief>create();
        mRuleAgentTable = HashMultimap.<InferenceRule, Belief>create();
    }

    public Set<Sentence> getFactSet() {
        return mFactAgentTable.keySet();
    }

    public Set<InferenceRule> getRuleSet() {
        return mRuleAgentTable.keySet();
    }

    /**
     * Set the belief.
     * TODO: replace the old belief.
     * @param agent
     * @param rule
     * @param level
     */
    public void believe(Agent agent, Belief belief) {
        mBeliefTable.put(agent, belief);
    }


    public void believe(Agent agent,
            Sentence fact,
            Measure measure) {
        Belief belief = BeliefFactory.createBelief(agent, fact, measure);
        believe(agent, belief);
        mFactAgentTable.put(fact, belief);

    }

    public void believe(Agent agent, InferenceRule rule, Measure measure) {
        Belief belief = BeliefFactory.createBelief(agent, rule, measure);
        believe(agent, belief);
        mRuleAgentTable.put(rule, belief);

    }

    public void believe(Agent agent, Sentence fact, double level) {
        Belief belief = BeliefFactory.createBelief(agent, fact, level);
        believe(agent, belief);
        mFactAgentTable.put(fact, belief);

    }

    public void believe(Agent agent, InferenceRule rule, double level) {
        Belief belief = BeliefFactory.createBelief(agent, rule, level);
        believe(agent, belief);
        mRuleAgentTable.put(rule, belief);

    }

    public Set<Belief> getBeliefSet(Object obj) {
        if (obj instanceof InferenceRule)
        {
            return getBeliefSet( (InferenceRule) obj);
        }else if (obj instanceof Sentence)
        {
            return getBeliefSet( (Sentence) obj);
        }else
        {
            return null;
        }
//        return (Set<Belief>) mRuleAgentTable.get(rule);
    }

    public Set<Belief> getBeliefSet(Sentence fact) {

        return (Set<Belief>) mFactAgentTable.get(fact);
    }


    public Set<Belief> getBeliefSet(InferenceRule rule) {
        return (Set<Belief>) mRuleAgentTable.get(rule);
    }

    public Set<Belief> getBeliefSet(Agent agent) {
        return (Set<Belief>) mBeliefTable.get(agent);
    }
}
